package es

import (
	"github.com/olivere/elastic/v7"
)

// -------------------------------------------- Buckets Metrics -------------------------------------------
// 封装了一些常用桶聚合, 深入操作请直接看源码调用
// 参考网址: https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/362tong-ju540828-bucketaggregations
// 参考网址: https://www.cnblogs.com/wangzhuxing/p/9581947.html
// 邻接矩阵聚合 分组聚合
func (m *Model) AdjacencyMatrixAggregation(name string, queryOptions map[string]elastic.Query) *Model {
	r := elastic.NewAdjacencyMatrixAggregation()
	for k, v := range queryOptions {
		r.Filters(k, v)
	}
	return m.SubAggregation(name, r)
}

// 自动日期直方图聚合
func (m *Model) AutoDateHistogramAggregation(name, field string, buckets int) *Model {
	return m.SubAggregation(name, elastic.NewAutoDateHistogramAggregation().Field(field).Format("yyyy-MM-dd").Buckets(buckets))
}

// 日期直方图聚合
func (m *Model) DateHistogramAggregation(name, field, interval string, keyed bool) *Model {
	return m.SubAggregation(name, elastic.NewDateHistogramAggregation().Field(field).Interval(interval).Format("yyyy-MM-dd").Keyed(keyed))
}

// 日期范围聚合
func (m *Model) DateRangeAggregation(name, field string, keyed bool, dateOptions map[string]interface{}) *Model {
	r := elastic.NewDateRangeAggregation().Field(field)
	for k, v := range dateOptions {
		r.AddRange(k, v)
	}
	return m.SubAggregation(name, r.Format("yyyy-MM-dd").Keyed(keyed))
}

// 过滤器聚合
func (m *Model) FilterAggregation(name string, filter elastic.Query) *Model {
	return m.SubAggregation(name, elastic.NewFilterAggregation().Filter(filter))
}

// 多过滤器聚合
func (m *Model) FiltersAggregation(name string, dateOptions map[string]elastic.Query) *Model {
	r := elastic.NewFiltersAggregation()
	for _, v := range dateOptions {
		r.Filters(v)
	}
	return m.SubAggregation(name, r)
}

// 地理距离聚合
func (m *Model) GeoDistanceAggregation(name, field, point string, numOptions map[int]int) *Model {
	r := elastic.NewGeoDistanceAggregation().Field(field).Point(point)
	for k, v := range numOptions {
		r.AddRange(k, v)
	}
	return m.SubAggregation(name, r)
}

// GeoHash网格聚合
func (m *Model) GeoHashGridAggregation(name, field string, precision interface{}) *Model {
	return m.SubAggregation(name, elastic.NewGeoHashGridAggregation().Field(field).Precision(precision))
}

// 直方图聚合
func (m *Model) HistogramAggregation(name, field string, interval float64) *Model {
	return m.SubAggregation(name, elastic.NewHistogramAggregation().Field(field).Interval(interval))
}

// IP范围聚合
func (m *Model) IPRangeAggregation(name, field, point string, ipOptions map[string]string, keyed bool) *Model {
	r := elastic.NewIPRangeAggregation().Field(field)
	for k, v := range ipOptions {
		r.AddRange(k, v)
	}
	return m.SubAggregation(name, r.Keyed(keyed))
}

// 丢失字段聚合
func (m *Model) MissingAggregation(name, field string) *Model {
	return m.SubAggregation(name, elastic.NewMissingAggregation().Field(field))
}

// 范围聚合
func (m *Model) RangeAggregation(name, field string, numOptions map[int]int) *Model {
	r := elastic.NewRangeAggregation().Field(field)
	for k, v := range numOptions {
		r.AddRange(k, v)
	}
	return m.SubAggregation(name, r)
}

// 桶聚合
func (m *Model) TermsAggregation(name, field string) *Model {
	return m.SubAggregation(name, elastic.NewTermsAggregation().Field(field))
}
